<!-- 印章授权审核 -->
<template>
	<view class="page">
		<u--form labelPosition="top" :model="model" ref="form" :rules="rules" labelWidth="auto" :labelStyle="{
				fontSize: '14px',
				color: '#B2B2B2',
				lineHight: '20px'
			}">
			<view class="basic-style">
				<view class="basic-title basic-border">新增</view>
				<u-form-item label="申请人" prop="handlerId" :borderBottom="false" required>
					<h-select-person-input style="width: 100%" disabled v-model="model.handlerId" placeholder="请选择申请人"
						border="surround" suffixIcon="arrow-down-fill" :suffixIconStyle="{color: '#B2B2B2'}"
						objectField="model.handlerId"></h-select-person-input>
				</u-form-item>
				<u-form-item label="申请部门" prop="handlerDeptId" :borderBottom="false" required>
					<h-select-depart-input disabled v-model="model.handlerDeptId" :departName="model.handlerDeptText"
						placeholder="请选择部门" border="surround" style="width: 100%" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						:title.sync="model.handlerDeptText"></h-select-depart-input>
				</u-form-item>
				<u-form-item label="申请时间" prop="createTime" :borderBottom="false" required>
					<u--input v-model="model.createTime" disabled placeholder="请选择申请时间" border="surround"
						suffixIcon="calendar-fill" :suffixIconStyle="{color: '#B2B2B2'}"></u--input>
				</u-form-item>
				<u-form-item label="单据编号" prop="applyCode" :borderBottom="false" required>
					<u--input disabled placeholder="请输入单据编号" v-model="model.applyCode"></u--input>
				</u-form-item>
				<u-form-item label="印章所属公司" prop="sealCompany" :borderBottom="false" required>
					<h-select-company-input style="width: 100%;" v-model="model.sealCompany"
						objectField="model.sealCompany" placeholder="请选择印章所属公司" border="surround"
						suffixIcon="arrow-down-fill" :suffixIconStyle="{color: '#B2B2B2'}" @input="onCompanyChange"></h-select-company-input>
				</u-form-item>
				<u-form-item label="选择印章" prop="ids" :borderBottom="false" required>
					<h-select-seal-input style="width: 100%;" v-model="model.ids" objectField="model.ids"
						placeholder="请选择印章" border="surround" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}" :sealCompany="model.sealCompany">
					</h-select-seal-input>
				</u-form-item>
				<template>
					<SealFormInfo
						v-for="(item, index) in formData"
						:key="index"
					  :item="item"
					  :index="index"
					/>
				</template>
				<u-form-item label="申请授权公司" prop="applyCompany" :borderBottom="false" required>
					<h-select-company-input style="width: 100%;" v-model="model.applyCompany"
						objectField="model.applyCompany" placeholder="请选择申请授权公司" border="surround"
						suffixIcon="arrow-down-fill" :suffixIconStyle="{color: '#B2B2B2'}" @input="onCompany"></h-select-company-input>
				</u-form-item>
				<u-form-item label="申请授权部门" prop="applyDept" :borderBottom="false" required>
					<h-select-depart-input v-model="model.applyDept" :departName="model.applyDept_dictText"
						placeholder="请选择申请授权部门" border="surround" style="width: 100%" suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
						:title.sync="model.applyDept_dictText"></h-select-depart-input>
				</u-form-item>
				<u-form-item label="申请授权责任人" prop="applyAuthPerson" required>
						<h-select-person-input
						  style="width: 100%"
						  v-model="model.applyAuthPerson"
						  placeholder="请选择人员"
						  border="surround"
						  suffixIcon="arrow-down-fill"
						  :suffixIconStyle="{ color: '#B2B2B2' }"
						  objectField="model.applyAuthPerson"
						></h-select-person-input>
				</u-form-item>
				<u-form-item label="被委托方(印章保管人)" prop="trustee" required>
						<h-select-person-input
						  style="width: 100%"
						  v-model="model.trustee"
						  placeholder="请选择人员"
						  border="surround"
						  suffixIcon="arrow-down-fill"
						  :suffixIconStyle="{ color: '#B2B2B2' }"
						  objectField="model.trustee"
						></h-select-person-input>
				</u-form-item>
				<u-form-item label="授权印章使用期限" prop="authDateStart" :borderBottom="false" required>
					<h-calendar-datetime-select
						style="width: 100%"
						v-model="authDate"
						@confirm="onCalendarConfirm"
						placeholder="请选择授权印章使用期限"
					></h-calendar-datetime-select>
				</u-form-item>
				<u-form-item label="项目名称" prop="projectName" :borderBottom="false">
					<h-select-project-input
						v-model="model.projectName" 
						style="width: 100%" 
						placeholder="请选择项目名称"
						objectField="model.projectName" 
						border="surround" 
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{ color: '#B2B2B2' }"
					></h-select-project-input>
				</u-form-item>
				<u-form-item label="授权范围及授权说明" prop="scopeContent" :borderBottom="false" ref="item1" required>
					<u--textarea v-model="model.scopeContent" :confirmType="null" count placeholder="请输入授权范围及授权说明"
						:maxlength="500"></u--textarea>
				</u-form-item>
			</view>
			<view class="basic-style">
				<view class="basic-title basic-border">附件信息</view>
				<view class="py-1 space-y-1" style="width: 100%">
					<view style="color: red">提示：请等待附件上传完成，再点击保存，否则附件无法保存成功！</view>
					<h-upload-file v-model="model.upload" :isIcon="false" />
					<file-list v-if="model.upload" v-model="model.upload" :isDelete="true"></file-list>
				</view>
			</view>
			<view class="space-x-1 px-1 btn_position">
				<u-button :disabled="isSubmit" type="primary" :plain="true" text="保存" @click="onDraft"></u-button>
				<u-button :disabled="isSubmit"  type="primary"  text="保存并提交" @click="onSubmit"></u-button>
			</view>
			<view style="height: 100rpx;"></view>
		</u--form>
		
		<u-toast ref="uToast"></u-toast>
		<ProcessNodeConfiguration ref="processConfig" @ok="onOk"></ProcessNodeConfiguration>
	</view>
</template>

<script>
	import fileList from '@/pages/synergy/components/fileList.vue'
	import SealFormInfo from './components/SealFormInfo.vue'
	import moment from 'moment'
	import ProcessNodeConfiguration from '@/components/ProcessNodeConfiguration/ProcessNodeConfiguration.vue' // 流程节点配置人员选择弹窗
	import {
		generatedFormNumber, // 获取单据编号
		queryByIdQualification, // 详情
		addSealAuth,
		editSealAuth,
		queryByIdQualificationChild,
	} from '@/api/index.js'
	export default {
		components: {
			fileList,
			SealFormInfo,
			ProcessNodeConfiguration
		},
		data() {
			return {
				isSubmit: false,
				id: '',
				model: {
					handlerId: '', // 申请人id
					handlerDeptText: '', //申请人
					handlerDeptId: '', // 申请部门id
					createTime: '', // 申请时间
					applyCode: '', // 单据编号
					sealCompany: '', // 印章所属公司
					ids: '', // 选择的印章id
					applyCompany: '', //申请授权公司
					applyAuthPerson: '', //申请授权责任人
					applyDept: '', //申请授权部门id
					applyDept_dictText: '', //申请授权部门
					trustee: '', //被委托方（印章保管人）
					authDateStart: '', //印章使用期限开始
					authDateEnd: '', //印章使用期限结束
					scopeContent: '', //授权范围及授权说明
					projectName: '', // 项目名称
					upload: null, //上传的文件
				},
				formData: [], // 印章列表
				authDate: [], //印章使用期限
				rules: {
					applyCode: [{ required: true, message: '请输入单据编号!', trigger: ['blur', 'change'] }],
					handlerDeptId: [{ required: true, message: '请选择申请部门!', trigger: ['blur', 'change'] }],
					handlerId: [{ required: true, message: '请选择申请人!', trigger: ['blur', 'change'] }],
					createTime: [{ required: true, message: '请选择申请时间!', trigger: ['blur', 'change'] }],
					
					sealCompany: [{
						required: true,
						message: '请选择印章所属公司!',
						trigger: 'change'
					}],
					ids: [{
						required: true,
						message: '请选择印章!',
						trigger: 'change'
					}],
					applyCompany: [{
						required: true,
						message: '请选择申请授权公司!',
						trigger: 'change'
					}],
					applyDept: [{
						required: true,
						message: '请选择申请授权部门!',
						trigger: 'change'
					}],
					applyAuthPerson: [{
						required: true,
						message: '请选择人员!',
						trigger: 'change'
					}],
					trustee: [{
						required: true,
						message: '请选择人员!',
						trigger: 'change'
					}],
					authDateStart: [{
						required: true,
						message: '请选择授权印章使用期限!',
						trigger: 'change'
					}],
					scopeContent: [{
						required: true,
						message: '请输入授权范围及授权说明!',
						trigger: ['change', 'blur']
					}],
				},
				loading: false,
				
				//代码生成后需手动设置流程编码
				flowCode: 'dev_erp_seal_qualification_authorization_audit_001',
				formUrl: '/erp/administration/seal/modules/ErpSealQualificationAuthorizationAuditForm',
				formUrlMobile: '/pages/synergy/sealQualification',
				processId: '1475998267459284993',
				beanName: 'sealAuthorizationFlow'
			}
		},
		onLoad(options) {
			if (options.id) {
				// 编辑
				this.id = options.id
				this.edit(options.id)
				
			} else {
				// 添加
				this.add()
			}

		},
		watch: {
			'model.ids': {
				handler (val) {
					this.getChildData(val)
				},
				immediate: true
			}
		},

		methods: {
			// 获取印章列表
			getChildData(ids){
				queryByIdQualificationChild({ ids  }).then(res => {
					if(res.success){
						this.formData = res.result || []
						if (res.result && res.result.length) {
							var ids_dictText = ''
							res.result.map(value => {
								ids_dictText = ids_dictText  + ',' + value.sealType
							})
							ids_dictText = ids_dictText.slice(1) 
							this.formData.ids_dictText = ids_dictText
						}
						
					}
				})
			},
			// 添加
			add() {
				this.getGeneratedFormNumber()
				this.model.createTime = moment().format('YYYY-MM-DD HH:mm'); // 申请时间
				this.model.handlerId = uni.getStorageSync('userInfo').empId; //申请人id
				this.model.handlerDeptId = uni.getStorageSync('userInfo').deptId; //申请部门id
			},
			// 编辑
			async edit(id) {
			  const response = await queryByIdQualification({ id: this.id })
			  const data = response?.result || {}
			  this.model = await Object.assign({}, data)
				this.authDate = [this.model.authDateStart, this.model.authDateEnd]
			},
			// 印章所属公司
			onCompanyChange (item) {
				this.$set(this.model, 'ids', '')
			},
			onCompany(item){
				this.$set(this.model,'companyLevel',item.companyLevel)
			},
			// 获取单据编号
			getGeneratedFormNumber() {
				generatedFormNumber({
					prefix: 'YZSQ',
					subSystemCode: '04'
				}).then(res => {
					if (res.success) {
						this.$set(this.model, 'applyCode', res.result)
						this.$set(this.model, 'bpmBizTitle',uni.getStorageSync('userInfo').employee.name+'-'+res.result)
					}
				})
			},
			// 选择印章使用期限
			onCalendarConfirm(val) {
				if (val && val.length && val.length == 2) {
					this.$set(this.model, 'authDateStart', val[0])
					this.$set(this.model, 'authDateEnd', val[val.length - 1])
					this.authDate = [this.model.authDateStart, this.model.authDateEnd]

				}
			},
			
			// 保存提交
			async onSubmit() {
				const val = await this.$refs.form.validate()
				if (!val) return
				this.loading = true
				var url = ''
				if (this.id) {
					url = editSealAuth
				} else {
					url = addSealAuth
				}
				this.isSubmit = true
				url(this.model).then(response => {
					this.$u.toast(response.message)
					if (response.success) {
						var that = this
						
						// 获取流程列表
						if (this.id) {
							this.getProcessList(this.id)
						} else {
						// 判断添加的时候是否有返回id值
							if (response.result) {
								this.getProcessList(response.result)
							}
						
						}
						this.$refs.uToast.show({
							message: response.message,
							type: 'default',
							complete () {
								// that.isSubmit = false
							}
						})
					}
				}).finally(() => this.loading = false)
			},
			
			async onDraft() {
			  // const val = await this.$refs.form.validate()
			  // if (!val) return
				var url = ''
			  if (this.id) {
			  	url = editSealAuth
			  } else {
			  	url = addSealAuth
			  }
			  
			  this.loading = true
				this.isSubmit = true
			  url(this.model).then(response => {
			  	if (response.success) {
			  		this.$refs.uToast.show({
			  			message: response.message,
			  			type: 'default',
			  			complete () {
			  				this.isSubmit = false
			  				if (getCurrentPages().length > 1) {
			  					uni.navigateBack()
			  				} else {
			  					uni.reLaunch({
			  						url: '/pages/home/home'
			  					})
			  				}
			  				
			  			}
			  		})
			  	} else {
						this.$refs.uToast.show({
							message: response.message,
							type: 'error',
						})
					}
			  }).catch(err => {
					this.$refs.uToast.show({
						message: err.message,
						type: 'error',
					})
				})
				.finally(() => this.loading = false, this.isSubmit = false)
			},
			// 获取流程列表
			getProcessList (id) {
				if (this.isSubmit) {
					this.$refs.processConfig.showPopup = true
					this.$refs.processConfig.formUrl = this.formUrl
					this.$refs.processConfig.formUrlMobile = this.formUrlMobile
					this.$refs.processConfig.flowCode = this.flowCode
					this.$refs.processConfig.edit(id, this.processId, this.beanName)
				}
			},
			// 提交流程成功
			onOk () {
				this.$refs.uToast.show({
					message: '提交成功',
					type: 'success',
					complete () {
						if (getCurrentPages().length > 1) {
							uni.navigateBack()
						} else {
							uni.reLaunch({
								url: '/pages/home/home'
							})
						}
						
					}
				})
			}
		}
	}
</script>


<style lang="scss" scoped>
	@import url("@/assets/inputStyle.scss");
</style>